#include <QComboBox>
#include <QCheckBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QKeyEvent>
#include <QMetaObject>
#include <QSpacerItem>
#include <QDebug>

#include "QyReplaceDialog.h"

QyReplaceDialog::QyReplaceDialog( QWidget * parent ) : QDialog(parent) {

	QWidget * lineBox = new QWidget(this);
	QWidget * optionBox = new QWidget(this);
	QWidget * buttonBox = new QWidget(this);
	QVBoxLayout * lineBoxLayout = new QVBoxLayout(lineBox);
	QHBoxLayout * optionBoxLayout = new QHBoxLayout(optionBox);
	QHBoxLayout * buttonBoxLayout = new QHBoxLayout(buttonBox);
	QPushButton * replaceOneButton = new QPushButton( tr("Replace"), buttonBox ),
		* replaceAllButton = new QPushButton( tr("Replace All"), buttonBox ),
		* replaceInSelectionButton = new QPushButton( tr("In Selection"), buttonBox ),
		* cancelButton = new QPushButton( tr("Cancel"), buttonBox );

	findTextEdit = new QComboBox(lineBox);
	replaceTextEdit = new QComboBox(lineBox);
	QVBoxLayout * boxLayout = new QVBoxLayout(this);
	regExpBox = new QCheckBox( tr("Reg Exp"), optionBox );
	caseSensitiveBox = new QCheckBox( tr("Case Sensitive"), optionBox );
	wordMatchesBox = new QCheckBox( tr("Word Matches"), optionBox );

	boxLayout->addWidget(lineBox);
	boxLayout->addWidget(optionBox);
	boxLayout->addWidget(buttonBox);
	lineBoxLayout->addWidget(findTextEdit);
	lineBoxLayout->addWidget(replaceTextEdit);
	lineBoxLayout->setMargin(0);
	optionBoxLayout->addWidget(regExpBox);
	optionBoxLayout->addWidget(caseSensitiveBox);
	optionBoxLayout->addWidget(wordMatchesBox);
	optionBoxLayout->setMargin(0);
	buttonBoxLayout->addWidget(replaceOneButton);
	buttonBoxLayout->addWidget(replaceAllButton);
	buttonBoxLayout->addWidget(replaceInSelectionButton);
	buttonBoxLayout->addWidget(cancelButton);
	buttonBoxLayout->addItem( new QSpacerItem( 20, 10, QSizePolicy::Expanding ) );
	buttonBoxLayout->setMargin(0);

	replaceOneButton->setObjectName("replaceOneButton");
	replaceAllButton->setObjectName("replaceAllButton");
	replaceInSelectionButton->setObjectName("replaceInSelectionButton");
	cancelButton->setObjectName("cancelButton");

	findTextEdit->setEditable(true);
	replaceTextEdit->setEditable(true);

	setWindowTitle( tr("Replace dialog") );
	QMetaObject::connectSlotsByName(this);

}

QString QyReplaceDialog::findText() const {

	return findTextEdit->currentText();
}

QString QyReplaceDialog::replaceText() const {

	return replaceTextEdit->currentText();
}

void QyReplaceDialog::setFindText( const QString & str ) {

	findTextEdit->setEditText(str);
	findTextEdit->setFocus();

}

void QyReplaceDialog::setReplaceText( const QString & str ) {

	replaceTextEdit->setEditText(str);

}

void QyReplaceDialog::on_replaceOneButton_clicked() {

	QString findText( findTextEdit->currentText() );

	if ( findText.isEmpty() )
		return;

	updateComboBoxes();

	emit replaceOne( findText, replaceTextEdit->currentText(),
		regExpBox->checkState() == Qt::Checked,
		caseSensitiveBox->checkState() == Qt::Checked,
		wordMatchesBox->checkState() == Qt::Checked
	);

}

void QyReplaceDialog::on_replaceAllButton_clicked() {

	QString findText( findTextEdit->currentText() );

	if ( findText.isEmpty() )
		return;

	updateComboBoxes();
	close();

	emit replaceAll( findText, replaceTextEdit->currentText(),
		regExpBox->checkState() == Qt::Checked,
		caseSensitiveBox->checkState() == Qt::Checked,
		wordMatchesBox->checkState() == Qt::Checked
	);
}

void QyReplaceDialog::on_replaceInSelectionButton_clicked() {

	QString findText( findTextEdit->currentText() );

	if ( findText.isEmpty() )
		return;

	updateComboBoxes();
	close();

	emit replaceInSelection( findText, replaceTextEdit->currentText(),
		regExpBox->checkState() == Qt::Checked,
		caseSensitiveBox->checkState() == Qt::Checked,
		wordMatchesBox->checkState() == Qt::Checked
	);
}

void QyReplaceDialog::on_cancelButton_clicked() {

	close();

}

void QyReplaceDialog::updateComboBoxes() {

	if ( findTextEdit->findText( findTextEdit->currentText() ) < 0 )
		findTextEdit->addItem( findTextEdit->currentText() );

	if ( replaceTextEdit->findText( replaceTextEdit->currentText() ) < 0 )
		replaceTextEdit->addItem( replaceTextEdit->currentText() );

}

void QyReplaceDialog::keyPressEvent( QKeyEvent * event ) {

	int key = event->key(),
		modifiers = event->modifiers();

	if ( key == Qt::Key_Escape && modifiers == Qt::NoModifier )
		close();

}
